package com.iwhalecloud.uncc.mq.handler;


import com.iwhalecloud.uncc.common.SpringContext;
import com.iwhalecloud.uncc.dao.ArchiverDao;
import com.iwhalecloud.uncc.db.DatasourceContextHolder;
import com.iwhalecloud.uncc.domain.ArchiverMsg;
import com.iwhalecloud.uncc.domain.InsCollectTaskArchiverMsg;
import com.iwhalecloud.uncc.mq.listener.IMqHandle;
import com.iwhalecloud.uncc.utils.ArchiverUtil;
import com.iwhalecloud.uncc.utils.CheckUtil;
import com.iwhalecloud.uncc.utils.HisUtil;
import lombok.extern.slf4j.Slf4j;

import java.util.List;
import java.util.Map;


/**
 * @author zq
 * @date 2021/10/12 16:42
 * @description InsCollectTaskArchiverHandler
 */
@Slf4j
public class InsCollectTaskArchiverHandler implements IMqHandle , ArchiverHandler{

    private ArchiverDao insCollectTaskArchiverDao;
    private ArchiverDao insCollectTaskBatchArchiverDao;
    private ArchiverDao insDataLogArchiverDao;
    private ArchiverDao insDistributeFileLogArchiverDao;
    private ArchiverDao insHttpCmdLogArchiverDao;
    private ArchiverDao insTaskFileLogArchiverDao;
    private ArchiverDao insTacheRecordArchiverDao;
    private ArchiverDao insTacheInstArchiverDao;
    private ArchiverDao insTacheLogArchiverDao;
    private ArchiverDao collectVirCommandInstArchiverDao;
    private ArchiverDao collectCommandInstArchiverDao;
    private ArchiverDao insEventRecordArchiverDao;

    @Override
    public void init(Map<String, Object> params) throws Exception {
        insCollectTaskArchiverDao = (ArchiverDao) getBean("insCollectTaskArchiverDao");
        insCollectTaskBatchArchiverDao = (ArchiverDao) getBean("insCollectTaskBatchArchiverDao");
        insDataLogArchiverDao = (ArchiverDao) getBean("insDataLogArchiverDao");
        insDistributeFileLogArchiverDao = (ArchiverDao) getBean("insDistributeFileLogArchiverDao");
        insHttpCmdLogArchiverDao = (ArchiverDao) getBean("insHttpCmdLogArchiverDao");
        insTaskFileLogArchiverDao = (ArchiverDao) getBean("insTaskFileLogArchiverDao");
        insTacheRecordArchiverDao = (ArchiverDao) getBean("insTacheRecordArchiverDao");
        insTacheInstArchiverDao = (ArchiverDao) getBean("insTacheInstArchiverDao");
        insTacheLogArchiverDao = (ArchiverDao) getBean("insTacheLogArchiverDao");
        collectVirCommandInstArchiverDao = (ArchiverDao) getBean("collectVirCommandInstArchiverDao");
        collectCommandInstArchiverDao = (ArchiverDao) getBean("collectCommandInstArchiverDao");
        insEventRecordArchiverDao = (ArchiverDao) getBean("insEventRecordArchiverDao");
    }

    private Object getBean(String beanName) {
        return SpringContext.getContext().getBean(beanName);
    }

    @Override
    public boolean dealMessage(List messages) {
        long s = System.currentTimeMillis();
        if (log.isInfoEnabled()) {
            log.info(Thread.currentThread().getName()+ "-InsCollectTaskArchiverHandler开始消费"+messages.size()+"条InsCollectTaskArchiverMsg");
        }

        ArchiverUtil.dealMessage(messages,InsCollectTaskArchiverMsg.class,this);

//        try {
//            for (int i = 0; i < messages.size(); i++) {
//                Object object = messages.get(i);
//                if (object instanceof MessageExt) {
//                    deal(((MessageExt) object).getBody());
//                } else if (object instanceof MQResult) {
//                    deal(((MQResult) object).getMessage().getBody());
//                }
//            }
//        }catch (Exception e) {
//            e.printStackTrace();
//        }finally {
//            DatasourceContextHolder.setDs(ArchiverConstant.DB_DEFAULT_HIS);
//        }

        if (log.isInfoEnabled()) {
            log.info(Thread.currentThread().getName() + "消费完,耗时:" + ((System.currentTimeMillis() - s) / 1000) + "s");
        }

        return true;
    }

//    private void deal(byte[] body) {
//        String message = new String(ZipUtil.uzip(body), StandardCharsets.UTF_8);
//        String[] strings = message.split(ArchiverConstant.MSG_SEPERATOR);
//        InsCollectTaskArchiverMsg msg = JsonUtil.json2Obj(strings[0], InsCollectTaskArchiverMsg.class);
//        String uuid = strings[1];
//        //处理msg
//        dealArchiverMsg(msg);
//    }

    @Override
    public void dealArchiverMsg(ArchiverMsg msg) {
        this.dealArchiverMsg((InsCollectTaskArchiverMsg)msg);
    }


    private void dealArchiverMsg(InsCollectTaskArchiverMsg msg) {
        if(msg.getInsCollectTaskList() == null){
            return;
        }
        doArchive(msg);
        isDeleteOnline(msg);
    }

    private void doArchive(InsCollectTaskArchiverMsg msg) {
        String newPartitionCode = HisUtil.changeHisDatasource(msg.getPartitionCode());
        CheckUtil.batchInsert(insCollectTaskArchiverDao,msg.getInsCollectTaskList(),newPartitionCode);
        CheckUtil.batchInsert(insCollectTaskBatchArchiverDao,msg.getInsCollectTaskBatchList(),newPartitionCode);
        CheckUtil.batchInsert(insDataLogArchiverDao,msg.getInsDataLogList(),newPartitionCode);
        CheckUtil.batchInsert(insDistributeFileLogArchiverDao,msg.getInsDistributeFileLogList(),newPartitionCode);
        CheckUtil.batchInsert(insHttpCmdLogArchiverDao,msg.getInsHttpCmdLogList(),newPartitionCode);
        CheckUtil.batchInsert(insTacheRecordArchiverDao,msg.getInsTacheRecordList(),newPartitionCode);
        CheckUtil.batchInsert(insTaskFileLogArchiverDao,msg.getInsTaskFileLogList(),newPartitionCode);
        CheckUtil.batchInsert(insTacheInstArchiverDao,msg.getInsTacheInstList(),newPartitionCode);
        CheckUtil.batchInsert(insTacheLogArchiverDao,msg.getInsTacheLogList(),newPartitionCode);
        CheckUtil.batchInsert(collectVirCommandInstArchiverDao,msg.getCollectVirCommandInstList(),newPartitionCode);
        CheckUtil.batchInsert(collectCommandInstArchiverDao,msg.getCollectCommandInstList(),newPartitionCode);
        CheckUtil.batchInsert(insEventRecordArchiverDao,msg.getInsEventRecordList(),newPartitionCode);
    }

    private void isDeleteOnline(InsCollectTaskArchiverMsg msg) {
        //切换oldPartitionCode 修改删除
        DatasourceContextHolder.setDs(msg.getPartitionCode());
        if(msg.getIsDeleteOnline()){
            CheckUtil.batchDelete(insCollectTaskArchiverDao,msg.getInsCollectTaskList());
            CheckUtil.batchDelete(insCollectTaskBatchArchiverDao,msg.getInsCollectTaskBatchList());
            CheckUtil.batchDelete(insDataLogArchiverDao,msg.getInsDataLogList());
            CheckUtil.batchDelete(insDistributeFileLogArchiverDao,msg.getInsDistributeFileLogList());
            CheckUtil.batchDelete(insHttpCmdLogArchiverDao,msg.getInsHttpCmdLogList());
            CheckUtil.batchDelete(insTacheRecordArchiverDao,msg.getInsTacheRecordList());
            CheckUtil.batchDelete(insTaskFileLogArchiverDao,msg.getInsTaskFileLogList());
            CheckUtil.batchDelete(insTacheInstArchiverDao,msg.getInsTacheInstList());
            CheckUtil.batchDelete(insTacheLogArchiverDao,msg.getInsTacheLogList());
            CheckUtil.batchDelete(collectVirCommandInstArchiverDao,msg.getCollectVirCommandInstList());
            CheckUtil.batchDelete(collectCommandInstArchiverDao,msg.getCollectCommandInstList());
            CheckUtil.batchDelete(insEventRecordArchiverDao,msg.getInsEventRecordList());
        }else{
            CheckUtil.batchUpdateArchived(insCollectTaskBatchArchiverDao,msg.getInsCollectTaskBatchList());
            CheckUtil.batchUpdateArchived(insCollectTaskArchiverDao,msg.getInsCollectTaskList());
            CheckUtil.batchUpdateArchived(insEventRecordArchiverDao,msg.getInsEventRecordList());
        }
    }
}
